//
//  TaskCell.m
//  RRQS
//
//  Created by kyjun on 16/3/24.
//
//

#import "TaskCell.h"

@interface TaskCell ()
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelTitle;
@property(nonatomic,strong) UILabel* labelContent;
@property(nonatomic,strong) UILabel* labelDistance;
@property(nonatomic,strong) UIImageView* line;
@property(nonatomic,strong) UILabel* labelLiuShui;

@property(nonatomic,strong) UIButton* addressIcon;

@property (nonatomic, assign) BOOL isWaimOrPaot;// YES跑腿       NO 外卖

@end

@implementation TaskCell


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    
    [self addSubview:self.btnIcon];
    [self addSubview:self.labelTitle];
//    [self addSubview:self.labelDistance];
    [self addSubview:self.labelContent];
    [self addSubview:self.line];
    [self addSubview:self.labelLiuShui];
    [self addSubview:self.addressIcon];

    
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    
    [self.btnIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.width.mas_equalTo(25);
        make.left.equalTo(self).offset(10);
    }];
    
    [self.addressIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.width.mas_equalTo(25);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.mas_equalTo(25);
        make.left.equalTo(self.btnIcon.mas_right).offset(10);
        make.right.equalTo(self.addressIcon.mas_left).offset(-10);
        
    }];
    
    
//    [self.labelDistance mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self);
//        make.right.equalTo(self.mas_right).offset(-10);
//    }];
    
    [self.labelLiuShui mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.labelTitle.mas_top);
        make.left.equalTo(self.btnIcon.mas_right).offset(10);
        make.right.equalTo(self.addressIcon.mas_left).offset(-10);

    }];
    
    
    [self.labelContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelTitle.mas_bottom);
        make.left.right.equalTo(self.labelLiuShui);
    }];
    
    
   
    
    
    
}


#pragma mark =====================================================  property package
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList // 跑腿
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        self.isWaimOrPaot = YES;

        _labelTitle.text = waiMaiDodelHList.title;
        _labelContent.text = waiMaiDodelHList.get_address;
        
//        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelHList.title,waiMaiDodelHList.get_address];
//        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
//        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:waiMaiDodelHList.title]];
//        _labelContent.attributedText = attributeStr;
        
        
        NSString *stra = [WMHelper getTheCorrectNum:waiMaiDodelHList.location_get];

        if (stra.length == 0) {
            
            _labelDistance.text = @"取货";
            
        }else{
            
            NSString *strMoney = [NSString stringWithFormat:@"%@km取货",stra];
            NSMutableAttributedString *attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
            [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:[NSString stringWithFormat:@"%@km",stra]]];
            _labelDistance.attributedText = attributeStr;
        }

        self.btnIcon.selected = YES;
        self.labelDistance.hidden = NO;
        
        self.labelLiuShui.text = [NSString stringWithFormat:@"流水号:%@",waiMaiDodelHList.liushui];
    }
}
- (void)setWaiMaiDodelListitems:(WJWaiMaiDodelListitems *)waiMaiDodelListitems // 外卖赋值
{
    if(waiMaiDodelListitems){
        _waiMaiDodelListitems = waiMaiDodelListitems;
        self.isWaimOrPaot = NO;

        _labelTitle.text = waiMaiDodelListitems.site_name;
        _labelContent.text = waiMaiDodelListitems.shopaddress;
        
       
//        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelListitems.site_name,waiMaiDodelListitems.shopaddress];
//        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
//        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:waiMaiDodelListitems.site_name]];
//        _labelContent.attributedText = attributeStr;
        
        
        if (waiMaiDodelListitems.location.length == 0) {
            
            _labelDistance.text = @"取货";
            
        }else{
            
        NSString *stra = [WMHelper getTheCorrectNum:waiMaiDodelListitems.location];
        NSString *strMoney = [NSString stringWithFormat:@"%@km取货",stra];
        NSMutableAttributedString *attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:[NSString stringWithFormat:@"%@km",stra]]];
        _labelDistance.attributedText = attributeStr;
            
        }
        
        
        self.btnIcon.selected = YES;
        self.labelDistance.hidden = NO;
        self.labelLiuShui.text = [NSString stringWithFormat:@"流水号:%@",waiMaiDodelListitems.liushui];

    }
}


-(void)setEntity:(MSubOrder *)entity{
    if(entity){
        _entity = entity;
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n%@",entity.storeName,entity.storeAddress];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strMoney rangeOfString:entity.storeName]];
        _labelContent.attributedText = attributeStr;
        strMoney = [NSString stringWithFormat:@"%@km取货",entity.location];
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:[NSString stringWithFormat:@"%@km",entity.location]]];
        _labelDistance.attributedText = attributeStr;
        if([entity.sendStatus isEqualToString:@"1"]){
            self.btnIcon.selected = YES;
            self.labelDistance.hidden = NO;
        }else {
            if (entity.status == expressStatusIng ){
                self.btnIcon.selected = YES;
            }else if(entity.status == expressStatusWating ){
                self.btnIcon.selected = YES;
            }else if (entity.status == expressStatusFinished ){
                self.btnIcon.selected = NO;
            }else if (entity.status == expressStatusCancelled ){
            }
        }
        
    }
}

-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-default"] forState:UIControlStateNormal];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateSelected];
    }
    return _btnIcon;
}

-(UILabel *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[UILabel alloc]init];
        _labelTitle.font = [UIFont systemFontOfSize:15.f];
        _labelTitle.numberOfLines = 0;
        _labelTitle.lineBreakMode = NSLineBreakByCharWrapping;
        _labelTitle.textColor =  [UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0];
    }
    return _labelTitle;
}


-(UILabel *)labelContent{
    if(!_labelContent){
        _labelContent = [[UILabel alloc]init];
        _labelContent.font = [UIFont systemFontOfSize:14.f];
        _labelContent.numberOfLines = 0;
        _labelContent.lineBreakMode = NSLineBreakByCharWrapping;
        _labelContent.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelContent;
}

-(UILabel *)labelDistance{
    if(!_labelDistance){
        _labelDistance = [[UILabel alloc]init];
        _labelDistance.font = [UIFont systemFontOfSize:12.f];
        _labelDistance.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
        _labelDistance.textAlignment = NSTextAlignmentRight;
    }
    return _labelDistance;
}

-(UILabel *)labelLiuShui{
    if(!_labelLiuShui){
        _labelLiuShui = [[UILabel alloc]init];
        _labelLiuShui.font = [UIFont systemFontOfSize:12.f];
        _labelLiuShui.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelLiuShui;
}

-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor =  [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _line;
}


-(UIButton *)addressIcon{
    if(!_addressIcon){
        _addressIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addressIcon setImage:[UIImage imageNamed:@"icon_address"] forState:UIControlStateNormal];
        [_addressIcon addTarget:self action:@selector(addressBtnTouch) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressIcon;
}

-(void)addressBtnTouch{
    
    if (self.addressBlock) {
        
        self.addressBlock(self.isWaimOrPaot, self.waiMaiDodelHList, self.waiMaiDodelListitems);
    }
}



@end
